package com.chebaohui.iqs.service.impl.deal.libao;

import java.util.Date;
import java.util.List;

import com.chebaohui.iqs.config.CompanyCode;
import com.chebaohui.iqs.config.CompanyValue;
import com.chebaohui.iqs.config.ResultCode;
import com.chebaohui.iqs.entity.base.Cars;
import com.chebaohui.iqs.entity.req.CheckCar;
import com.chebaohui.iqs.entity.res.CompanyCars;
import com.chebaohui.iqs.service.impl.deal.libao.profess.LbCheckCar;

/**
 * 利宝查车线程
 * 
 * @author xielonghai
 *
 */
public class LiBaoCarThread extends Thread {
	private CheckCar carParam;// 查车参数

	private CompanyCars<List<Cars>> car;// 车辆查询结果

	private long timeDeal = 0l;// 线程处理时间

	public LiBaoCarThread(CheckCar carParam) {
		super();
		this.carParam = carParam;
	}

	/**
	 * 线程启动
	 */
	@Override
	public void run() {
		long begin = new Date().getTime();
		car = LbCheckCar.tpCheckCar(carParam);
		timeDeal = new Date().getTime() - begin;
	}

	/**
	 * 获取查车结果
	 * 
	 * @return
	 */
	public CompanyCars<List<Cars>> getResult() {
		int time = 0;
		while (true) {
			if (!this.isAlive() || time >= CompanyValue.maxTime)
				break;

			time++;
			try {
				Thread.sleep(1000);// 每隔1秒查看线程是否完成
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		try {
			if (this.isAlive()) {// 关闭线程
				this.interrupt();
				this.join();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		if (null == car) {
			car = new CompanyCars<List<Cars>>(CompanyCode.LIBAO.getCode(), ResultCode.N02.getCode(),
					ResultCode.N02.getMsg() + "利宝查车线程处理超时");
		}

		return car;
	}

	/**
	 * 线程处理时间
	 * 
	 * @return
	 */
	public long getTimeDeal() {
		return timeDeal;
	}
}
